package com.shujia.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object Demo1KakfaProducer {
  def main(args: Array[String]): Unit = {


    /**
      * 1、创建生产者
      *
      */

    val properties = new Properties()

    //1、kafka broker列表
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")



    val kafkaProducer = new KafkaProducer[String, String](properties)


    /**
      * 2、向kafka中生产数据
      *
      */

    //构建一行数据
    val record = new ProducerRecord[String, String]("words", "java")

    //发送数据到kafka
    kafkaProducer.send(record)

    //手动将数据刷到kafka
    kafkaProducer.flush()

    //关闭链接
    kafkaProducer.close()

  }

}
